import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/resource/resources.dart';
import 'package:lib_uikit/widgets/text/gt_text.dart';

///币种组件
class GTCurrencyV3 extends StatelessWidget {
  final List<GTCurrencyModalV3>? imageUrls;
  final String title;
  final String? subTitle;
  final String? bottomText;
  final String? multiple;

  const GTCurrencyV3({
    super.key,
    required this.imageUrls,
    required this.title,
    this.subTitle,
    this.bottomText,
    this.multiple,
  });

  @override
  Widget build(BuildContext context) {
    bool showSubImage = false;
    List<GTCurrencyModalV3> _imageUrls = imageUrls ?? [];
    if (_imageUrls.any((bean) => (bean.subImageUrl ?? "").isNotEmpty)) {
      showSubImage = true;
    }
    return Row(
      mainAxisSize: MainAxisSize.min,
      //单行文字居中，多行文字上对齐
      crossAxisAlignment: (bottomText ?? "").isNotEmpty ? CrossAxisAlignment.start : CrossAxisAlignment.center,
      children: [
        _imageContainer(showSubImage, _imageUrls),
        SizedBox(
          width: showSubImage ? 14 : 8,
        ),
        _titleContainer()
      ],
    );
  }

  Widget _titleContainer() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            GTText(
              title,
              removeTopBottomPadding: true,
              style: gtTextStyleW500(fontSize: 16, color: GTColorsV3.text1.color),
            ),
            if (subTitle != null) ...[
              SizedBox(
                width: (bottomText ?? "").isNotEmpty ? 2 : 4,
              ),
              GTText(
                subTitle ?? "",
                removeTopBottomPadding: true,
                style: gtTextStyleW400(fontSize: 12, color: GTColorsV3.text3.color),
              ),
            ],
          ],
        ),
        if ((bottomText ?? "").isNotEmpty) ...[
          const SizedBox(
            height: 4,
          ),
          Row(
            children: [
              if ((multiple ?? "").isNotEmpty) ...[
                Container(
                  padding: const EdgeInsets.symmetric(horizontal: 2),
                  decoration: BoxDecoration(
                    color: GTColorsV3.cmpt15.color,
                    borderRadius: BorderRadius.circular(2),
                  ),
                  child: GTText(
                    multiple ?? "",
                    removeTopBottomPadding: true,
                    style: gtTextStyleW600(fontSize: 10, color: GTColorsV3.text5.color),
                  ),
                ),
                const SizedBox(
                  width: 4,
                )
              ],
              GTText(
                bottomText ?? "",
                removeTopBottomPadding: true,
                style: gtTextStyleW400(fontSize: 12, color: GTColorsV3.text3.color),
              ),
            ],
          ),
        ]
      ],
    );
  }

  Widget _imageContainer(bool showSubImage, List<GTCurrencyModalV3> _imageUrls) {
    if (_imageUrls.isEmpty) return Container();
    return Wrap(
      spacing: showSubImage ? 0 : -8,
      children: _imageUrls.map((bean) {
        return Stack(
          clipBehavior: Clip.none,
          children: [
            GTImage(
              borderWidth: _imageUrls.length > 1 ? 1 : 0,
              borderColor: GTColorsV3.card2.color,
              width: 28,
              height: 28,
              url: bean.imageUrl,
              radiusAll: 14,
              showPlaceholder: true,
              fit: BoxFit.cover,
              errorPath: Images.icLendDefaultCoinIcon,
              placeholderPath: Images.icLendDefaultCoinIcon,
            ),
            if ((bean.subImageUrl ?? "").isNotEmpty) ...[
              Positioned(
                bottom: 0,
                right: -6,
                child: GTImage(
                  borderWidth: _imageUrls.length > 1 ? 1 : 0,
                  borderColor: GTColorsV3.card2.color,
                  width: 14,
                  height: 14,
                  url: bean.subImageUrl,
                  radiusAll: 7,
                  showPlaceholder: true,
                  fit: BoxFit.cover,
                  errorPath: Images.icLendDefaultCoinIcon,
                  placeholderPath: Images.icLendDefaultCoinIcon,
                ),
              ),
            ],
            if (bean.showStar ?? false) ...[
              const Positioned(
                bottom: 0,
                right: 0,
                child: GTImage(
                  assetPath: "packages/lib_uikit/assets/images/uikit_currency_star.png",
                  width: 11,
                  height: 11,
                  fit: BoxFit.cover,
                ),
              ),
            ],
          ],
        );
      }).toList(),
    );
  }
}

class GTCurrencyModalV3 {
  ///大图片地址
  final String imageUrl;

  ///小图片地址
  final String? subImageUrl;

  ///是否展示自选图标
  final bool? showStar;

  GTCurrencyModalV3({required this.imageUrl, this.subImageUrl, this.showStar});
}
